import 'package:flutter/material.dart';
import 'package:amap_location/amap_location.dart';

class LocationPage extends StatefulWidget {
  LocationPage({Key key}) : super(key: key);

  @override
  _LocationPageState createState() => _LocationPageState();
}

class _LocationPageState extends State<LocationPage> {

  TextEditingController _address = TextEditingController();
  TextEditingController _longitude = TextEditingController();
  TextEditingController _latitude = TextEditingController();

  @override
  void initState() {
    super.initState();
    _getLocation();
  }

  _getLocation() async{
    // 启动一下
    await AMapLocationClient.startup(new AMapLocationOption( desiredAccuracy:CLLocationAccuracy.kCLLocationAccuracyHundredMeters  ));
    // 获取地理位置
    var result = await AMapLocationClient.getLocation(true);
    print("经度:${result.longitude}, 纬度${result.latitude}");

    setState(() {
      this._latitude.text = result.latitude.toString();
      this._longitude.text = result.longitude.toString();
      this._address.text = result.formattedAddress;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('我的地理位置') 
      ), 
      body: Center(
        child: Container(
          width: double.infinity,
          padding: EdgeInsets.all(10),
          child: ListView(
            children: <Widget>[
              SizedBox(height: 40),
              TextField(
                controller: this._address,
                maxLines: 2,
                readOnly: true,
                decoration: InputDecoration(
                  border: OutlineInputBorder(),
                  labelText: '您现在的位置'
                ),
              ),
              SizedBox(height: 10),
              TextField(
                controller: this._longitude,
                readOnly: true,
                decoration: InputDecoration(
                  border: OutlineInputBorder(),
                  labelText: '经度'
                ),
              ),
              SizedBox(height: 10), 
              TextField(
                controller: this._latitude,
                readOnly: true,
                decoration: InputDecoration(
                  border: OutlineInputBorder(),
                  labelText: '纬度'
                ),
              )
            ],
          )
        ) 
      )
    );
  }
}